<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>使用Symbol定义protected属性</title>
  </head>
  <body></body>
  <script>
    const protecteds = Symbol();
    class User {
      constructor(name) {
        this[protecteds] = {};
        this[protecteds].host = "https://houdunren.com";
        this[protecteds].name = name;
      }
      set host(url) {
        if (!/^https?:\/\//i.test(url)) {
          throw new Error("地址错误");
        }
        this[protecteds].host = url;
      }
      get host() {
        return this[protecteds].host;
      }
      get name() {
        return this[protecteds].name;
      }
    }
    let hd = new User("后盾人");
    hd.host = "https://baidu.com";
    // hd.name = "向边";
    console.log(hd.name);
    console.log(hd.host);
    console.log(hd);
  </script>
</html>
